<?php
declare (strict_types = 1);

namespace hangpu8\hpcms\validate;

use think\Validate;

class Admin extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名' =>  ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'role_id'                   => 'require',
        'username'                  => 'require|min:4|max:20|alphaNum|unique:admin,username',
        'password'                  => 'require|min:6|max:20',
        'nickname'                  => 'require',
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名' =>  '错误信息'
     *
     * @var array
     */
    protected $message = [
        'role_id.require'           => ['code' => 1001, 'msg' => '请选择所属角色'],
        'username.require'          => ['code' => 1002, 'msg' => '请输入登录账号'],
        'username.min'              => ['code' => 1003, 'msg' => '账号最低必须6位数'],
        'username.max'              => ['code' => 1004, 'msg' => '账号最多可输入20位'],
        'username.alphaNum'         => ['code' => 1005, 'msg' => '账号必须为字母+数字'],
        'username.unique'           => ['code' => 1006, 'msg' => '该账号已注册'],
        'password.require'          => ['code' => 1007, 'msg' => '请输入登录密码'],
        'password.min'              => ['code' => 1008, 'msg' => '登录密码最低必须6位数'],
        'password.max'              => ['code' => 1009, 'msg' => '登录密码最多可输入20位'],
        'nickname.require'          => ['code' => 2001, 'msg' => '请输入用户昵称'],
    ];

    // 验证场景
    protected $scene  = [
        'login'                     =>  ['username','password'],
        'add'                       =>  ['role_id','username','password','nickname'],
        'edit'                      =>  ['role_id','username','nickname'],
    ];

    /**
     * 登录场景验证规则添加或移除
     *
     * @return void
     */
    public function sceneLogin()
    {
        return $this
        ->remove('role_id', ['require'])
        ->remove('username', ['min','max','unique'])
        ->remove('password', ['min','max'])
        ->remove('nickname', ['require']);
    }

    /**
     * 数据更新场景规则添加或移除
     *
     * @return void
     */
    public function sceneEdit()
    {
        return $this
        ->remove('username', ['unique'])
        ->remove('password', ['require','min','max']);
    }
}
